package com.rc.evcharger.common.utils;

/**
 * Create by Yang on 2021/3/24
 */
public class JunDataUtil {

    public static String xor(String strHex_X, String strHex_Y) {
        //将x、y转成二进制形式
        String anotherBinary = Integer.toBinaryString(Integer.valueOf(strHex_X, 16));
        String thisBinary = Integer.toBinaryString(Integer.valueOf(strHex_Y, 16));
        String result = "";
        //判断是否为8位二进制，否则左补零
        if (anotherBinary.length() != 8) {
            for (int i = anotherBinary.length(); i < 8; i++) {
                anotherBinary = "0" + anotherBinary;
            }
        }
        if (thisBinary.length() != 8) {
            for (int i = thisBinary.length(); i < 8; i++) {
                thisBinary = "0" + thisBinary;
            }
        }
        //异或运算
        for (int i = 0; i < anotherBinary.length(); i++) {
            //如果相同位置数相同，则补0，否则补1
            if (thisBinary.charAt(i) == anotherBinary.charAt(i))
                result += "0";
            else {
                result += "1";
            }
        }
        return Integer.toHexString(Integer.parseInt(result, 2));
    }


    public static void main(String[] args) throws Exception {
        String str = "5A A5 00 10 11 00 66 66 11 53 44 00 00 11 02 0C 00 03 02 02 EE FF 20 21 03 24 19 07 57";
        System.out.println(checkcode_0007(str.split(" ")));
    }

    public static String checkcode_0007(String[] dateArr) {
        //int length = para.length() / 2;
        //String[] dateArr = new String[length];
        //
        //for (int i = 0; i < length; i++) {
        //    dateArr[i] = para.substring(i * 2, i * 2 + 2);
        //}
        String code = "00";
        for (int i = 0; i < dateArr.length; i++) {
            code = xor(code, dateArr[i]);
        }
        if (code.length() == 1) {
            return "0" + code;
        }
        return code.toUpperCase();
    }
}
